"""
目的 文件按行读取 生成sql将状态置0或者1
要求文件每行代表数据库中的某个feild
根据条件做出相应的操作 单文件 且假设文件很大
"""
from abc import ABC, abstractmethod

from facade.mysqlfacade import MysqlUtiles
from xjlibrary.our_file_dir import BaseDir
from xjlibrary.tools.BaseString import BaseString


class FileToSqlDeal(ABC):
    """
    该类提供一个文件到数据库的转换处理
    同时兼容sql语句写入文件
    """

    def __init__(self):
        self.linelist = []

    def file_to_sql_set_status(self, filepath, dealnum=10000):
        """

        :param filepath: 文件路径
        :param dealnum:  每次处理的量
        :return:
        """
        linenum = 0
        for line in BaseDir.read_file_r_mode_yield(filepath):
            self.linelist.append(line)
            linenum += 1
            if linenum >= dealnum:
                self.file_line_hook()
                yield self.create_sql()
                linenum = 0
                self.linelist.clear()
        self.file_line_hook()
        print("文件处理完毕")
        yield self.create_sql()
        linenum = 0
        self.linelist.clear()

    def file_line_hook(self):
        """
        对数据的处理层 重写完成对数据的清洗
        :param linelist:
        :return:
        """
        return self.linelist

    @abstractmethod
    def create_sql(self):
        """
        这里就生成了sql 返回
        :return:
        """
        pass

    def sql_to_file(self, filepath, sql):
        """
        将sql语句记录到文件
        :param filepath:
        :param sql:
        :return:
        """
        BaseDir.single_add_file(filepath, sql + "\n")

    def exe_file_sql_to_mysql(self, configfile, sesc, filepath):
        # 配置文件与默认配置文件key的转换
        keytransformdicts = {"port": "db_port", "host": "db_host", "passwd": "db_pw", "user": "db_user",
                             "db": "db_name",
                             "chartset": "db_charset"}
        # mysql 工具类
        myutil = MysqlUtiles(configfile, sesc, keytransformdicts=keytransformdicts)
        # 从sql文件路径读取sql并执行
        for sSql in BaseDir.read_file_r_mode_yield(filepath):
            myutil.ExeSqlToDB(sSql, errExit=True)


class FileToSqlDeal2(FileToSqlDeal):

    def file_line_hook(self):
        """
        对数据的处理层 重写完成对数据的清洗
        :param linelist:
        :return:
        """
        listnew = []
        for line in self.linelist:
            listnew.append(BaseString.r_strip_substring(line, "\n"))
        self.linelist = listnew
        return self.linelist

    def create_sql(self):
        """
        这里就生成了sql 返回
        :return:
        """
        sql = "update `article` set `stat`=1 where `doi` in {}".format(tuple(self.linelist))
        return sql


def file_to_sql_set_status_test():
    # # 读取文件的位置
    # filepath = BaseDir.get_new_path("F:\\", "filetest.txt")
    # # 保存sql语句的位置
    sqlfilepath = BaseDir.get_new_path("F:\\", "sql.txt")
    # # 移除原来的sql语句
    # BaseDir.remove_file(sqlfilepath)
    # # 处理类
    # fts = FileToSqlDeal2()
    # # 处理类生成sql语句
    # for sql in fts.file_to_sql_set_status(filepath):
    #     # 保存到文件
    #     fts.sql_to_file(sqlfilepath, sql)

    fts = FileToSqlDeal2()
    curpath = BaseDir.get_file_dir_absolute(__file__)
    configfile = BaseDir.get_new_path(curpath, "db.ini")
    # 执行sql语句到数据库
    fts.exe_file_sql_to_mysql(configfile, "dbfiledeal", sqlfilepath)


file_to_sql_set_status_test()
